﻿Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp

Namespace My
    Namespace Connector
        <HideModuleName()> _
        Module PhlogiosConnector
            Private _PhlogiosClient As Base.IPhlogios
            Private Logger As NLog.Logger = NLog.LogManager.GetCurrentClassLogger

            Sub Init()
                Logger.Debug("Init Done = " & InitializationDone)
                If InitializationDone Then Exit Sub

                Dim ServerName As String = "localhost"

                Dim channelOptions As IDictionary = New Hashtable()
                channelOptions("socketCacheTimeout") = "1000001"

                Dim chan As TcpClientChannel = New TcpClientChannel(channelOptions, New BinaryClientFormatterSinkProvider)

                If ChannelServices.GetChannel(chan.ChannelName) IsNot Nothing Then
                    ChannelServices.UnregisterChannel(ChannelServices.GetChannel(chan.ChannelName))
                End If

                ChannelServices.RegisterChannel(chan, False)
                _PhlogiosClient = Activator.GetObject(GetType(Base.IPhlogios), String.Format("tcp://{0}:{1}/{2}", ServerName, My.Settings.ServerPort, My.Settings.RemotingName))

                InitializationDone = True
            End Sub

            Private InitializationDone As Boolean

            Sub New()
                InitializationDone = False
                Init()
            End Sub

            Public ReadOnly Property Client() As Base.IPhlogios
                Get
                    Return _PhlogiosClient
                End Get
            End Property

            Public WindowSize As Size
            Public WindowLocation As Point
            Public WindowState As FormWindowState

        End Module
    End Namespace
End Namespace